package com.sum.common.utils;

public class StringUtil {
    public static void Str2Hex(String buf, int len, byte[] hexbuf) {
        String chex = "0123456789ABCDEF";
        if (len != 0) {
            for(int i = 0; i < len; i += 2) {
                byte hnx = (byte)chex.indexOf(buf.toUpperCase().substring(i, i + 1));
                byte lnx = 0;
                if (i + 2 <= len) {
                    lnx = (byte)chex.indexOf(buf.toUpperCase().substring(i + 1, i + 2));
                }

                hexbuf[i % 2 == 0 ? i / 2 : i / 2 + 1] = (byte)(hnx << 4 & 255 | lnx & 255);
            }

        }
    }

    public static void Hex2Str(byte[] buf, int len, char[] out) {
        char[] hexc = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

        for(int i = 0; i < len; ++i) {
            out[i * 2] = hexc[(buf[i] & 255) / 16];
            if (i * 2 + 1 < out.length) {
                out[i * 2 + 1] = hexc[(buf[i] & 255) % 16];
            }
        }

    }
}
